package org.jivesoftware.webchat.util;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class Files  {
	
	public void upFiles()throws UnknownHostException, IOException{
		
	    ServerSocket server = new ServerSocket(9091);
			
		Socket clientSocket = null;
			
		while (true) {
			
				clientSocket = server.accept();
				
				InputStream is = clientSocket.getInputStream();
				
				DataInputStream dis = new DataInputStream(is);
				
				String fileName = dis.readUTF();
				
				System.out.println("filename:" + fileName);
				
				File path = new File("c:\\tomcat6\\webapps\\webchat\\files\\");
				
				File file = new File(path, fileName);
				
				file.createNewFile();
	
				FileOutputStream fos = new FileOutputStream(file);
				
				byte[] b = new byte[1024];
				
				int i = 0;
				
				while ((i = dis.read(b)) != -1) {
					fos.write(b, 0, i);
				}
				
				fos.close();
				
				dis.close();
				
				clientSocket.close();
				
				
			
		   }
		
	  
	
   }

}